<!--
 * @Description: 1分钱商品专区列表
 * @Author: Li Yujie
 * @Date: 2020/12/22
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/22
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    1分钱商品专区
                </div>
            </div>
            <!-- /header -->
            <div class="btn-view">
                <div>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="handleGoodsEdit('', 'add')"
                    >
                        添加1分钱专区商品
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="$refs.goodsSelect.show(2, requestUrl, typeList)"
                    >
                        批量添加商品
                    </el-button>
                </div>
            </div>
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">商品信息：</p>
                    <el-input
                        placeholder="请输入商品名称或商品编号"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        style="width: 300px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">上架状态：</p>
                    <el-select
                        v-model="filterData.goods_status_free"
                        clearable
                        filterable
                        placeholder="全部"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-left: 32px"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>

            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
            >
                <el-table-column
                    type="index"
                    label="序号"
                    width="80"
                    align="center"
                />
                <el-table-column
                    prop="goods_no"
                    label="商品编号"
                    show-overflow-tooltip
                    align="left"
                    width="150"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_title_free"
                    label="商品名称"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_title"
                    label="所链自营商品"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            class="text-button"
                            @click="toGoodsList(scope.row.goods_title)"
                        >
                            {{ scope.row.goods_title }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_price"
                    label="原售价"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_sort_free"
                    label="排序号"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="上架状态"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.goods_status_free === 1">已上架</div>
                        <div v-else-if="scope.row.goods_status_free === 0">待上架</div>
                        <div v-else>--</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="添加时间"
                    show-overflow-tooltip
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        <div>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_buy_limit_free"
                    label="每月限领"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    prop="user_count"
                    label="领取用户"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    label="活动设置总库存"
                    show-overflow-tooltip
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        {{ scope.row.total_stock }}
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="活动剩余总库存"
                    show-overflow-tooltip
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        {{ scope.row.total_rest_stock }}
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否平台补贴"
                    show-overflow-tooltip
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="240"
                    align="left"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="handleGoodsEdit(scope.row.goods_no, 'edit')"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            @click="handleGoodsOption(scope.row)"
                        >
                            {{ scope.row.goods_status_free === 1 ? '下架' : '上架' }}
                        </el-button>
                        <el-button
                            type="text"
                            @click="handleGoodsDelete(scope.row.goods_no)"
                        >
                            删除
                        </el-button>
                        <el-button
                            type="text"
                            @click="toOrderDetail(scope.row.goods_no)"
                        >
                            购买详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!-- 查看价格弹窗 -->
        <el-dialog
            title="查看商品"
            :visible.sync="productVisible"
            width="780px"
            :close-on-click-modal="false"
        >
            <el-table
                :data="productList"
                height="100%"
                style="width: 100%;max-height: 450px"
                class="data-table"
            >
                <el-table-column
                    width="240"
                    label="规格"
                >
                    <template slot-scope="scope">
                        {{ scope.row.spec_list | filterSpec }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="product_stock_free_origin"
                    label="活动设置库存"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    label="活动剩余库存"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.product_stock_free === -1 ? scope.row.product_stock : scope.row.product_stock_free }}
                    </template>
                </el-table-column>
                <el-table-column
                    property="name"
                    align="center"
                    label="是否平台补贴"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.shop_subsidy_enable === 1">平台补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 0">商家全部补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 2">商家部分补贴</div>
                    </template>
                </el-table-column>
                <el-table-column
                    property="shop_subsidy_money_by_shop"
                    align="center"
                    label="商家补贴金额"
                    :formatter="emptyFormatter"
                />
            </el-table>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="productVisible = false"
                    size="medium"
                    type="primary"
                >
                    我知道了
                </el-button>
            </span>
        </el-dialog>
        <!--商品选择弹窗-->
        <goods-select
            ref="goodsSelect"
            @change="handleSelectGoods"
        />
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import GoodsSelect from '@/components/goods/goods-select';

export default {
    name: "ZeroAreaList",
    data() {
        return {
            filterData: {
                keyword: '',
                goods_status_free: ''
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: ''},
                {label: '已上架', value: 1},
                {label: '已下架', value: 0}
            ],
            // 商品弹窗类型
            typeList: [
                {label: '自营', value: 1},
                {label: '折扣', value: 5},
            ],
            requestUrl: '/student/shop_vip_free%5Cget_depot_goods_list',
            authResult: {},
            productList: [],
            productVisible: false
        };
    },
    mixins: [back],
    filters: {
        filterSpec (val) {
            let str = '';

            val.forEach((el, n) => {
                str += el.spec_name + ':' + el.spec_value + (n == val.length - 1 ? '' : ';');
            });

            return str;
        }
    },
    components: {
        GoodsSelect,
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '1分钱商品专区'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {

        /**
         * 获取表格数据
         * @param page
         */
        getTableData(page) {
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_vip_free%5Cget_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '失败',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },
        /**
         * 跳转编辑商品
         * @param no
         * @param type add 新增 edit 编辑
         */
        handleGoodsEdit(no = '', type) {

            this.$router.push({
                path: '/student/zero-goods-edit',
                query: {
                    no,
                    type,
                }
            });
        },

        /**
         * 删除专区商品
         * @param no
         */
        handleGoodsDelete(no) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: no
                };

                this.$post('/student/shop_vip_free%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('删除成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 查看购买详情
         * @param no
         */
        toOrderDetail(no) {

            let routeData = this.$router.resolve({
                path: '/student/order-list',
                query: {
                    orderType: 9
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 获取批量选择的商品
         * @param val
         */
        handleSelectGoods(val) {
            this.$router.push({
                path: '/student/zero-goods-edit-batch',
                query: {
                    goodsList: JSON.stringify(val)
                }
            });
        },

        /**
         * 商品上下架
         * @param row
         */
        handleGoodsOption(row) {
            if (!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm(`确认${row.goods_status_free === 1 ? '下架' : '上架'}该商品吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: row.goods_no,
                    goods_status_free: row.goods_status_free === 1 ? 0 : 1
                };

                this.$post('/student/shop_vip_free%5Cmodify', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('操作成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 跳转商品列表
         * @param title
         */
        toGoodsList(title) {
            let routeData = this.$router.resolve({
                name: 'GoodsList',
                query: {
                    goods_title: title,
                    goods_type: 'self'
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 显示弹窗
         * @param row
         */
        showPriceDialog(row) {
            let params = {
                goods_no: row.goods_no,
            };

            this.$post('/student/shop_vip_free%5Cget_product_list', params, resp => {
                if (resp.code === 1) {
                    this.productList = resp.data.product_list.filter(item => {
                        return item.check_status === 1;
                    });
                    this.productVisible = true;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        }
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        .btn-view {
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 24px 24px 0;
        }

        /*筛选*/

        .filter-view {
            margin: 24px 0;
            display: flex;
            align-items: center;

            .header-new-box {
                display: flex;
                align-items: center;
                color: #606266;
                white-space: nowrap;
                margin-left: 32px;

                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }

        .status-wrapper {
            display: flex;
            align-items: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #C2C2C2;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 32px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }

        .text-button {
            width: 180px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            text-align: left;
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }
}
</style>
